package cqrtplm.entity;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Entity
@Table(name = "rt_dept_role")
@Data
public class DeptRoleDO {
    @Id
    private String oid;
    private String deptId;
    private String deptName;
    private String roleName;
    private String roleId;
    private String userName;
    private String userId;
    private String userNo;
    private Date createstamp;
    private Date updatestamp;
    private Date modifystamp;
    private Long updatecount;
    private Long markfordelete;

    public static void fillCommonFieldForUpdate(DeptRoleDO entityBaseDO, Date date) {
        entityBaseDO.setUpdatestamp(date);
        entityBaseDO.setModifystamp(date);
        if (entityBaseDO.getUpdatecount() == null) {
            entityBaseDO.setUpdatecount(0L);
        }

        entityBaseDO.setUpdatecount(entityBaseDO.getUpdatecount() + 1L);
    }

    public static void fillCommonFieldForCreate(DeptRoleDO entityBaseDO, Date date) {
        entityBaseDO.setCreatestamp(date);
        entityBaseDO.setUpdatestamp(date);
        entityBaseDO.setModifystamp(date);
        entityBaseDO.setUpdatecount(0L);
        entityBaseDO.setMarkfordelete(0L);
    }

    public static Map<String,DeptRoleDO> convertToDeptRoleDOMap(JSONArray deptRoleArray){
        Map<String,DeptRoleDO> map = new HashMap<>();
        for (int i = 0; i < deptRoleArray.size(); i++) {
            JSONObject json = deptRoleArray.getJSONObject(i);
            DeptRoleDO deptRoleDO = new DeptRoleDO();
            String deptId = json.getString("DPTID");
            if(deptId.startsWith("-")){
                deptId = deptId.replaceFirst("-", "");
            }
            deptRoleDO.setDeptId(deptId);
            deptRoleDO.setDeptName(json.getString("DptName"));
            String roleID = json.getString("RoleID");
            if(roleID.startsWith("-")){
                roleID = roleID.replaceFirst("-", "");
            }
            deptRoleDO.setRoleId(roleID);
            deptRoleDO.setRoleName(json.getString("RoleName"));
            String userID = json.getString("UserID");
            if(userID.startsWith("-")){
                userID = userID.replaceFirst("-", "");
            }
            deptRoleDO.setUserId(userID);
            deptRoleDO.setUserName(json.getString("UserName"));
            deptRoleDO.setUserNo(json.getString("UserNO"));
            String id = json.getString("ID");
            if(id.startsWith("-")){
                id = id.replaceFirst("-", "");
            }
            deptRoleDO.setOid(id);
            map.put(id,deptRoleDO);
        }
        return map;
    }
}
